package com.zb.service.impl;

import com.google.common.collect.Lists;
import com.zb.dto.HeapPoint;
import com.zb.dto.QuotaInfo;
import com.zb.dto.TrendPoint;
import com.zb.dto.TrendPoint2;
import com.zb.es.EsRepository;
import com.zb.influx.InfluxRepository;
import com.zb.service.ReportService;
import com.zb.vo.BoardQuotaData;
import com.zb.vo.BoardQuotaVO;
import com.zb.vo.PieVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private EsRepository esRepository;
    @Autowired
    private InfluxRepository influxRepository;

    @Override
    public BoardQuotaVO getBoardData(String start, String end, String quotaId, List<String> devices, int type) {
        BoardQuotaVO boardQuotaVO = new BoardQuotaVO();
        boardQuotaVO.setLegend(devices);
        boardQuotaVO.setYdata(Lists.newArrayList());

        for (String device : devices) {
            List<TrendPoint2> point2List = this.getQuotaTrend(start, end, quotaId, device, type);
            //存储x数据
            List<String> timeList = point2List.stream().map(item -> item.getTime()).collect(Collectors.toList());
            boardQuotaVO.setXdata(timeList);
            //存储Y数据
            BoardQuotaData boardQuotaData = new BoardQuotaData();
            boardQuotaData.setName(device);
            List<Long> valueList = point2List.stream().map(item -> item.getPointValue()).collect(Collectors.toList());
            boardQuotaData.setData(valueList);
            boardQuotaVO.getYdata().add(boardQuotaData);
        }
        return boardQuotaVO;
    }

    @Override
    public List<String> getDeviceByQuota(String quotaId) {
        String ql = "select distinct(deviceId) as deviceId from (select deviceId,value from quota where quotaId = '" + quotaId + "' group by deviceId , quotaId) limit 10 offset 0";
        List<QuotaInfo> list = influxRepository.query(ql, QuotaInfo.class);

        List<String> collect = list.stream().map(item -> item.getDeviceId())
                .collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<TrendPoint2> getQuotaTrend(String start, String end, String quotaId, String deviceId, int type) {
        StringBuffer ql = new StringBuffer("select last(value) as pointValue from quota where deviceId='" + deviceId + "' and quotaId='" + quotaId + "'  and time>='" + start + "' and time<='" + end + "'");
        if (type == 1) {
            ql.append(" group by time(1m)");
        }
        if (type == 2) {
            ql.append(" group by time(1h)");
        }
        if (type == 3) {
            ql.append(" group by time(1d)");
        }
        return influxRepository.query(ql.toString(), TrendPoint2.class);
    }


    @Override
    public List<PieVO> getStatusList() {
        Long count = esRepository.getAllDeviceCount();
        Long alarmCount = esRepository.getAlarmCount();
        Long offLineCount = esRepository.getOffLineCount();
        System.out.println(count - alarmCount - offLineCount);
        List<PieVO> list = new ArrayList<>();
        PieVO p1 = new PieVO();
        p1.setName("正常运转");
        p1.setValue(count - alarmCount - offLineCount);

        PieVO p2 = new PieVO();
        p2.setName("离线");
        p2.setValue(offLineCount);

        PieVO p3 = new PieVO();
        p3.setName("报警");
        p3.setValue(alarmCount);
        list.add(p1);
        list.add(p2);
        list.add(p3);
        return list;
    }

    @Override
    public List<TrendPoint> getAlarmTrend(String start, String end, int type) {
        StringBuffer ql = new StringBuffer("select count(value) from quota where alarm='1' ");
        ql.append(" and time>='" + start + "' and time<='" + end + "'");
        if (type == 1) {
            ql.append(" group by time(1m)");
        }
        if (type == 2) {
            ql.append(" group by time(1h)");
        }
        if (type == 3) {
            ql.append(" group by time(1d)");
        }
        System.out.println(ql.toString());
        List<TrendPoint> list = influxRepository.query(ql.toString(), TrendPoint.class);
        return list;
    }

    @Override
    public List<HeapPoint> getTop10Alarm(String start, String end) {
        StringBuffer ql = new StringBuffer("select top(count , deviceId, quotaId,quotaName ,10) as count from (select count(value) from quota where alarm='1' and time>='" + start + "' and time<='" + end + "' group by deviceId , quotaId , quotaName) order by desc ");
        return influxRepository.query(ql.toString(), HeapPoint.class);
    }


}
